package com.eastjet.equipment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eastjet.equipment.mapper.DeviceInspectiontaskMapper;
import com.eastjet.equipment.service.DeviceInspectiontaskService;
import com.eastjet.equipment.util.PageUtil;
import com.eastjet.equipment.util.RedisUtil;
import com.eastjet.maintenance.domain.DeviceInspectiontask;

import com.eastjet.maintenance.domain.DevicePatrolscheme;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 杨博文
 * @description 针对表【device_inspectiontask(巡检任务表)】的数据库操作Service实现
 * @createDate 2024-11-25 09:23:38
 */
@Service
public class DeviceInspectiontaskServiceImpl extends ServiceImpl<DeviceInspectiontaskMapper, DeviceInspectiontask>
        implements DeviceInspectiontaskService {

    @Autowired
    private DeviceInspectiontaskMapper deviceInspectiontaskMapper;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public String addTask(DeviceInspectiontask deviceInspectiontask) {
        System.out.println("deviceInspectiontask:" + deviceInspectiontask.toString());
        try {
            System.out.println("更新设备维修经验库");
            // 尝试将数据插入数据库
            if (deviceInspectiontaskMapper.insert(deviceInspectiontask) > 0) {
                // 定义Redis的键
                String redisKey = "TaskList:";
                try {
                    // 从数据库获取当前所有数据
                    String condition = "";
                    List<DeviceInspectiontask> deviceInspectiontaskList = deviceInspectiontaskMapper.selectList(null); // 可以根据需要替换为实际查询条件
                    // 将更新后的数据写入Redis
                    redisUtil.set(redisKey, deviceInspectiontaskList);
                } catch (Exception e) {
                    // 处理Redis操作异常
                    System.err.println("Redis更新失败: " + e.getMessage());
                    // 这里可以根据实际情况决定是否要抛出异常或者进行其他处理
                }
                return "添加成功";
            } else {
                return "添加失败";
            }
        } catch (Exception e) {
            // 处理数据库操作异常
            System.err.println("数据库操作失败: " + e.getMessage());
            return "添加失败";
        }
    }

    @Override
    public Page<DeviceInspectiontask> getTaskList(int page, int size, String condition, String name) {
        // 定义Redis中的键
        String redisKey = "TaskList:" + name;
        System.out.println("SchemeList-condition:" + condition);
        // 先查缓存Redis
        if (redisUtil.hasKey(redisKey)) {
            System.out.println(1);
            System.out.println("taskList-从Redis中获取数据");
            List<DeviceInspectiontask> TaskList;
            if (condition != null && !condition.isEmpty() && condition != "") {
                System.out.println(2);
                QueryWrapper<DeviceInspectiontask> qw = new QueryWrapper<>();
                qw.like("inspectionTask_name", condition).or().like("inspectionTask_code", condition);
                qw.eq("insert_person", name);
                qw.eq("is_delete", 0);
                TaskList = deviceInspectiontaskMapper.selectList(qw); // 获取所有数据
            } else {
                System.out.println(3);
                TaskList = (List<DeviceInspectiontask>) redisUtil.get(redisKey);
            }
            // 使用PageUtil进行分页
            return PageUtil.paginate(TaskList, page, size);
        } else {
            System.out.println(4);
            System.out.println("在数据库中查询TaskList");
            List<DeviceInspectiontask> TaskList;

            // 根据condition条件查询
            if (condition != null && !condition.isEmpty() && condition != "") {
                System.out.println(5);
                QueryWrapper<DeviceInspectiontask> qw = new QueryWrapper<>();
                qw.like("inspectionTask_name", condition).or().like("inspectionTask_code", condition);
                if (name != null && !name.isEmpty() && name != "") {
                    qw.eq("insert_person", name);
                }

                qw.eq("is_delete", 0);

                TaskList = deviceInspectiontaskMapper.selectList(qw); // 获取所有数据
            } else if (name != null && !name.isEmpty() && name != "") {
                System.out.println(6);
                condition = "";
                QueryWrapper<DeviceInspectiontask> qw = new QueryWrapper<>();
                qw.eq("is_delete", 0);
                qw.eq("insert_person", name);
                TaskList = deviceInspectiontaskMapper.selectList(qw); // 获取所有数据
                // 将数据存入Redis
                redisUtil.set(redisKey, TaskList);
            } else {
                System.out.println(6);
                condition = "";
                QueryWrapper<DeviceInspectiontask> qw = new QueryWrapper<>();
                qw.eq("is_delete", 0);
                TaskList = deviceInspectiontaskMapper.selectList(qw); // 获取所有数据
                // 将数据存入Redis
                redisUtil.set(redisKey, TaskList);
            }
            // 使用PageUtil进行分页
            return PageUtil.paginate(TaskList, page, size);
        }
    }

    @Override
    public String updateTask(DeviceInspectiontask deviceInspectiontask) {
        System.out.println("deviceInspectiontask:" + deviceInspectiontask.toString());
        try {
            System.out.println("更新设备维修经验库");
            // 尝试将数据插入数据库

            UpdateWrapper uw = new UpdateWrapper();
            uw.set("inspectionTask_type_id", "执行中");
            uw.set("update_time", new Date());
            uw.set("update_person", deviceInspectiontask.getUpdatePerson());
            uw.set("inspectionTask_time", deviceInspectiontask.getInspectiontaskTime());
            uw.set("inspectionTask_okontime", deviceInspectiontask.getInspectiontaskOkontime());
            uw.set("inspectionTask_okuntime", deviceInspectiontask.getInspectiontaskOkuntime());
            uw.set("inspectionTask_ontime", deviceInspectiontask.getInspectiontaskOkontime());
            uw.set("inspectionTask_untime", deviceInspectiontask.getInspectiontaskOkuntime());
            uw.eq("code", deviceInspectiontask.getCode());
            if (deviceInspectiontaskMapper.update(deviceInspectiontask, uw) > 0) {
                // 定义Redis的键
                String redisKey = "TaskList:";
                try {
                    // 从数据库获取当前所有数据
                    String condition = "";
                    List<DeviceInspectiontask> deviceInspectiontaskList = deviceInspectiontaskMapper.selectList(null); // 可以根据需要替换为实际查询条件
                    // 将更新后的数据写入Redis
                    redisUtil.set(redisKey, deviceInspectiontaskList);
                } catch (Exception e) {
                    // 处理Redis操作异常
                    System.err.println("Redis更新失败: " + e.getMessage());
                    // 这里可以根据实际情况决定是否要抛出异常或者进行其他处理
                }
                return "修改成功";
            } else {
                return "修改失败";
            }
        } catch (Exception e) {
            // 处理数据库操作异常
            System.err.println("数据库操作失败: " + e.getMessage());
            return "修改失败";
        }
    }

    @Override
    public String updateSkip(DeviceInspectiontask deviceInspectiontask) {
        System.out.println("skiptask:" + deviceInspectiontask.toString());
        try {
            System.out.println("更新设备维修经验库");
            // 尝试将数据插入数据库

            UpdateWrapper uw = new UpdateWrapper();
            uw.set("cause", deviceInspectiontask.getCause());
            uw.set("inspectionTask_skip", deviceInspectiontask.getInspectiontaskSkip());
            uw.eq("code", deviceInspectiontask.getCode());
            if (deviceInspectiontaskMapper.update(deviceInspectiontask, uw) > 0) {
                // 定义Redis的键
                String redisKey = "TaskList:";
                try {
                    // 从数据库获取当前所有数据
                    String condition = "";
                    List<DeviceInspectiontask> deviceInspectiontaskList = deviceInspectiontaskMapper.selectList(null); // 可以根据需要替换为实际查询条件
                    // 将更新后的数据写入Redis
                    redisUtil.set(redisKey, deviceInspectiontaskList);
                } catch (Exception e) {
                    // 处理Redis操作异常
                    System.err.println("Redis更新失败: " + e.getMessage());
                    // 这里可以根据实际情况决定是否要抛出异常或者进行其他处理
                }
                return "修改成功";
            } else {
                return "修改失败";
            }
        } catch (Exception e) {
            // 处理数据库操作异常
            System.err.println("数据库操作失败: " + e.getMessage());
            return "修改失败";
        }
    }


    @Override
    public String adjustTask(DeviceInspectiontask deviceInspectiontask) {
        System.out.println("deviceInspectiontask:" + deviceInspectiontask.toString());
        try {
            System.out.println("更新设备维修经验库");
            // 尝试将数据插入数据库
            UpdateWrapper uw = new UpdateWrapper();
            uw.set("inspectionTask_okontime", deviceInspectiontask.getInspectiontaskOkontime());
            uw.set("inspectionTask_okuntime", deviceInspectiontask.getInspectiontaskOkuntime());
            uw.set("cause", deviceInspectiontask.getCause());
            uw.set("update_time", new Date());
            uw.set("update_person", deviceInspectiontask.getUpdatePerson());
            uw.set("inspectionTask_class", deviceInspectiontask.getInspectiontaskClass());
            uw.set("inspectionTask_person", deviceInspectiontask.getInspectiontaskPerson());
            uw.eq("code", deviceInspectiontask.getCode());
            if (deviceInspectiontaskMapper.update(deviceInspectiontask, uw) > 0) {
                // 定义Redis的键
                String redisKey = "TaskList:";
                try {
                    // 从数据库获取当前所有数据
                    if (redisUtil.hasKey(redisKey)) {
                        redisUtil.del(redisKey);
                    }

                } catch (Exception e) {
                    // 处理Redis操作异常
                    System.err.println("Redis更新失败: " + e.getMessage());
                    // 这里可以根据实际情况决定是否要抛出异常或者进行其他处理
                }
                return "修改成功";
            } else {
                return "修改失败";
            }
        } catch (Exception e) {
            // 处理数据库操作异常
            System.err.println("数据库操作失败: " + e.getMessage());
            return "修改失败";
        }
    }
}




